﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace DataModels
{
    //TODO play more then one sound;
    public class SoundManager
    {
        public const int MAX_EFFECTS = 500;
        public const int MAX_SONGS = 200;
        public bool PLAY_MUSIC = true;
        public bool PLAY_EFFECTS = true;

        protected SoundEffect[] effectList = new SoundEffect[MAX_EFFECTS];
        protected String[] effectListNames = new String[MAX_EFFECTS];
        protected Song currentPlaySong;
        protected Song[] songList = new Song[MAX_SONGS];
        protected String[] songListNames = new String[MAX_SONGS];

        protected int currentEffect = 0;
        protected int currentSong = 0;

        public SoundManager()
        {
        }

        //private static SoundManager me= new SoundManager();

        //public static SoundManager getSoundManager()
        //{
        //    return me;
        //}

        public void addSong(Song setSong, String songName)
        {
            songList[currentSong] = setSong;
            songListNames[currentSong] = songName;
            currentSong++;
        }

        public void playSong(string songName)
        {
            playSong(getSong(songName));
        }

        public void stopSong()
        {
            MediaPlayer.Pause();
        }

        public void resumeSong()
        {
            MediaPlayer.Resume();
        }

        public void playSong(int index)
        {
            Song song = getSongAt(index);
            if (song != null && PLAY_MUSIC)
                playSong(song);
            currentPlaySong = song;
        }

        public Song getSongAt(int index)
        {
            if (index < MAX_EFFECTS && index >= 0)
            {
                return songList[index];
            }
            return null;
        }

        public Song getSong(String name)
        {
            for (int i = 0; i < MAX_SONGS; i++)
            {
                if (name.Equals(songListNames[i]))
                    return songList[i];
            }

            return null;
        }

        public void playSong(Song song)
        {
            if (song != null && PLAY_MUSIC)
            {
                MediaPlayer.Play(song);
                MediaPlayer.IsRepeating = true;
                MediaPlayer.Volume = 0.25f;
            }
        }





        public void addEffect(SoundEffect addEffect, String effectName)
        {
            effectList[currentEffect] = addEffect;
            effectListNames[currentEffect] = effectName;
            currentEffect++;
        }

        public SoundEffect getEffectAt(int index)
        {
            if (index < MAX_EFFECTS && index >= 0)
            {
                return effectList[index];
            }
            return null;
        }

        public void playEffect(int index)
        {
            SoundEffect eff = getEffectAt(index);
            if (eff != null && PLAY_EFFECTS)
                eff.Play();
        }

        public SoundEffect getEffect(string name)
        {
            for (int i = 0; i < MAX_EFFECTS; i++)
            {
                if (name.Equals(effectListNames[i]))
                    return effectList[i];
            }

            return null;
        }

        public void playEffect(string name)
        {
            SoundEffect eff = null;
            if (name != null && !name.Equals(""))
                eff = getEffect(name);


            if (eff != null && PLAY_EFFECTS)
            {
                eff.Play();
            }
        }

        public void playEffect(SoundEffect playMe)
        {
            TimeSpan time = playMe.Duration;
        }

    }
}
